package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.dao.BsRegiscomeinDao;
import io.renren.modules.app.entity.BsMemberEntity;
import io.renren.modules.app.entity.BsRegiscomeinEntity;
import io.renren.modules.app.entity.BsRegisgooutEntity;
import io.renren.modules.app.service.BsRegiscomeinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


@Service("bsRegiscomeinService")
@Slf4j
public class BsRegiscomeinServiceImpl extends ServiceImpl<BsRegiscomeinDao, BsRegiscomeinEntity> implements BsRegiscomeinService {

    @Resource
    private BsRegiscomeinDao bsRegiscomeinDao ;

    @Resource
    private BsMemberDao bsMemberDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsRegiscomeinEntity> wrapper1 = new LambdaQueryWrapper<>();

        if (params.get("memberName") != null && !params.get("memberName").toString().equals("")){
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(queryWrapper);
            List<String> list = new ArrayList<>();
            if (bsMemberEntities.size() > 0){
                for (BsMemberEntity bsMemberEntity : bsMemberEntities) {
                    list.add(bsMemberEntity.getMemberId());
                }
                wrapper1.in(BsRegiscomeinEntity::getMemberId,list);
            }else {
                IPage<BsRegiscomeinEntity> page = this.page(
                        new Query<BsRegiscomeinEntity>().getPage(params),
                        new LambdaQueryWrapper<BsRegiscomeinEntity>().eq(BsRegiscomeinEntity::getId,-1)
                );
                return new PageUtils(page);
            }

        }
        if (params.get("comeinName") != null && !params.get("comeinName").toString().equals("")){
            wrapper1.like(BsRegiscomeinEntity::getComeinName,params.get("comeinName"));
        }
        if (params.get("comeinPhone") != null && !params.get("comeinPhone").toString().equals("")){
            wrapper1.like(BsRegiscomeinEntity::getComeinPhone,params.get("comeinPhone"));
        }
        if (params.get("comeinIforder") != null && !params.get("comeinIforder").toString().equals("")){
            wrapper1.eq(BsRegiscomeinEntity::getComeinIforder,params.get("comeinIforder"));
        }
        if (params.get("comeinState") != null && !params.get("comeinState").toString().equals("")){
            wrapper1.eq(BsRegiscomeinEntity::getComeinState,params.get("comeinState"));
        }

        IPage<BsRegiscomeinEntity> page = this.page(
                new Query<BsRegiscomeinEntity>().getPage(params),
                wrapper1
        );
        for (BsRegiscomeinEntity record : page.getRecords()) {
            //会员姓名数据清洗
            LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsMemberEntity::getMemberId,record.getMemberId());
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(wrapper);
            if (bsMemberEntities.size() == 1){
                record.setMemberName(bsMemberEntities.get(0).getMemberName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    public void saveRegComein(BsRegiscomeinEntity bsRegiscomein) {
        try {
            //会员编号存在性校验
            LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsMemberEntity::getMemberId,bsRegiscomein.getMemberId());
            List<BsMemberEntity> entities = bsMemberDao.selectList(wrapper);
            if (entities.size() < 1){
                throw new RRException("会员编号不合法");
            }
            //电话号校验
            if (!phoneValidate(bsRegiscomein.getComeinPhone())){
                throw new RRException("电话号不合法");
            }
            Date comeinFacttime = bsRegiscomein.getComeinFacttime();//实际来访时间
            Date comeinOuttime = bsRegiscomein.getComeinOuttime();//离开时间
            if (comeinFacttime.getTime() > comeinOuttime.getTime()){
                throw new RRException("离开时间不能早于实际来访时间");
            }
            bsRegiscomeinDao.insert(bsRegiscomein);
        }catch (Exception e){
            log.info("报错的类及所在的方法：{}","BsRegiscomeinService --> saveRegComein");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateRegComein(BsRegiscomeinEntity bsRegiscomein) {
        try {
            BsRegiscomeinEntity entity1 = bsRegiscomeinDao.selectById(bsRegiscomein.getId());
            if (!entity1.getMemberId().equals(bsRegiscomein.getMemberId())){
                throw new RRException("会员编号不可修改");
            }
            //电话号校验
            if (!phoneValidate(bsRegiscomein.getComeinPhone())){
                throw new RRException("电话号不合法");
            }
            Date comeinFacttime = bsRegiscomein.getComeinFacttime();//实际来访时间
            Date comeinOuttime = bsRegiscomein.getComeinOuttime();//离开时间
            if (comeinFacttime.getTime() > comeinOuttime.getTime()){
                throw new RRException("离开时间不能早于实际来访时间");
            }
            bsRegiscomeinDao.updateById(bsRegiscomein);
        }catch (Exception e){
            log.info("报错的类及所在的方法：{}","BsRegiscomeinService --> updateRegComein");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    //校验电话号公共方法
    private boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);

            return matches;
        }
        return false;
    }


}
